#include "loggin.h"
#include <QScreen>
#include <QGuiApplication>

bool LogginWidget::iss_server_connected = false;

LogginWidget::LogginWidget(QWidget* parent):QWidget(parent)
{
    QScreen* screen = QGuiApplication::primaryScreen();
    QSize size = screen->size();
    this->setFixedSize(size);

    m_layout = new QGridLayout();
    m_layout->setAlignment(Qt::AlignCenter);
    m_layout->setContentsMargins(10, 10, 10, 50);

    m_iss_server = new QLabel();
    m_iss_server->setText("iss server");
    m_iss_server->setStyleSheet("background-color: rgb(8, 111, 160);");
    m_iss_server->setAlignment(Qt::AlignCenter);
    m_layout->addWidget(m_iss_server, 0, 0, 1, 2);

    m_iss_server_ip_label = new QLabel();
    m_iss_server_ip_label->setText("iss server ip: ");
    m_layout->addWidget(m_iss_server_ip_label, 1, 0, 1, 1);

    m_iss_server_ip_line = new QLineEdit();
    m_iss_server_ip_line->setText("192.168.2.3");
    m_layout->addWidget(m_iss_server_ip_line, 1, 1, 1, 1);

    m_iss_server_ip_port = new QLabel();
    m_iss_server_ip_port->setText("iss server port: ");
    m_layout->addWidget(m_iss_server_ip_port, 2, 0, 1, 1);

    m_iss_server_port = new QLineEdit();
    m_iss_server_port->setText("8080");
    m_layout->addWidget(m_iss_server_port, 2, 1, 1, 1);

    m_iss_user_name = new QLabel();
    m_iss_user_name->setText("user name: ");
    m_layout->addWidget(m_iss_user_name, 3, 0, 1, 1);

    m_iss_user_name_line = new QLineEdit();
    m_iss_user_name_line->setText("admin");
    m_layout->addWidget(m_iss_user_name_line, 3, 1, 1, 1);

    m_iss_user_passwd = new QLabel();
    m_iss_user_passwd->setText("user passwd: ");
    m_layout->addWidget(m_iss_user_passwd, 4, 0, 1, 1);

    m_iss_user_passwd_line = new QLineEdit();
    m_iss_user_passwd_line->setText("admin");
    m_iss_user_passwd_line->setEchoMode(QLineEdit::Password);
    m_layout->addWidget(m_iss_user_passwd_line, 4, 1, 1, 1);

    m_connect = new QPushButton();
    m_connect->setText("Disconnecting. Press to connect.");
    m_connect->setStyleSheet("background-color: rgb(128, 0, 0);");
    m_layout->addWidget(m_connect, 5, 0, 1, 2);

    m_layout->setRowStretch(0, 0);
    m_layout->setRowStretch(1, 0);
    m_layout->setRowStretch(2, 0);
    m_layout->setRowStretch(3, 0);
    m_layout->setRowStretch(4, 0);
    m_layout->setRowStretch(5, 0);

    m_layout->setColumnStretch(0, 0);
    m_layout->setColumnStretch(1, 0);

    qDebug() << "LogginWidget size: " << this->size();

    this->setLayout(m_layout);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    m_layout->setAlignment(Qt::AlignCenter);

    connect(m_connect, SIGNAL(released()), this, SLOT(connect_iss_server_check()));
}

void LogginWidget::connect_iss_server_check()
{
    if(!iss_server_connected) {
        m_connect->setText("Connecting. Press to disconnect.");
        m_connect->setStyleSheet("background-color: rgb(0, 255, 0);");
    } else {
        m_connect->setText("Disconnecting. Press to connect.");
        m_connect->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
    iss_server_connected = !iss_server_connected;
}

LogginWidget::~LogginWidget() {}
